package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.SysWxRoleMenu;
import com.hzhc.mapper.SysWxRoleMenuMapper;
import com.hzhc.service.SysWxRoleMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 微信小程序角色菜单管理 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2023-11-07
 */
@Service
@RequiredArgsConstructor
public class SysWxRoleMenuServiceImpl extends ServiceImpl<SysWxRoleMenuMapper, SysWxRoleMenu> implements SysWxRoleMenuService {
    private final SysWxRoleMenuMapper sysWxRoleMenuMapper;

    @Override
    public List<Long> getListIdsByRoleId(Long roleId) {
        List<Long> lret = new ArrayList<Long>();
        QueryWrapper<SysWxRoleMenu> queryWrapper = new QueryWrapper<SysWxRoleMenu>();
        queryWrapper.eq("wx_role_id", roleId);
        List<SysWxRoleMenu> list = sysWxRoleMenuMapper.selectList(queryWrapper);
        for (SysWxRoleMenu rm : list) {
            lret.add(rm.getWxMenuId());
        }
        return lret;
    }

    @Override
    public Boolean saveOrUpdateRoleMenu(ArrayList<Integer> data, Long roleid) {
        // 先删除后添加
        SysWxRoleMenu wxRoleMenu = new SysWxRoleMenu();
        wxRoleMenu.setWxRoleId(roleid);
        sysWxRoleMenuMapper.delete(new QueryWrapper<SysWxRoleMenu>(wxRoleMenu));
        for (int i = 0; i < data.size(); i++) {
            Integer mid = data.get(i);
            SysWxRoleMenu rolem = new SysWxRoleMenu();
            rolem.setWxRoleId(roleid);
            rolem.setWxMenuId(Long.valueOf(mid));
            sysWxRoleMenuMapper.insert(rolem);
        }
        return true;
    }
}
